/**
 * @file   csv_ops.cpp
 * @brief  
 * @encode UTF-8
 * @log    date        author      notes
 *         2021.09.17  Jimmy       first version
 */
#include <unistd.h>
#include <string.h>
#include "../inner_libs/usr/include/debug_log/log.h"
#include "csv_ops.h"

/**
 * @brief  Construct a new Csv Ops:: Csv Ops object
 * @param  path             
 * @param  type             
 * @author Jimmy 
 * @date   2021.09.17
 */
CsvOps::CsvOps(std::string path, std::string type)
{
    mPath = path;
    mFd   = fopen(path.c_str(), type.c_str());
    
    if (mFd == nullptr)
    {
        return;
    }
}

/**
 * @brief  Destroy the Csv Ops:: Csv Ops object
 * @author Jimmy 
 * @date   2021.09.17
 */
CsvOps::~CsvOps()
{
    if (mFd != nullptr)
    {
        fflush(mFd);
        fclose(mFd);
        mFd = nullptr;
    }            
}

/**
 * @brief  插入
 * @param  a                
 * @param  b                
 * @return true 
 * @return false 
 * @author Jimmy 
 * @date   2021.09.17
 */
bool CsvOps::append(int a, int b)
{
    if (mFd == nullptr)
    {
        return false;
    }

    char buf[10] = {0};
    sprintf(buf, "%d,%d\r\n", a, b);
    fwrite(buf, strlen(buf), 1, mFd);
    fflush(mFd);

    return true; 
}

/**
 * @brief  插入
 * @param  data             
 * @return true 
 * @return false 
 * @author Jimmy 
 * @date   2021.09.17
 */
bool CsvOps::append(std::vector<int> data)
{
    return true;
}

/**
 * @brief  删除文件
 * @return true 
 * @return false 
 * @author Jimmy 
 * @date   2021.09.17
 */
bool CsvOps::deleteFile()
{
    if (mFd == nullptr)
    {
        return false;
    }

    unlink(mPath.c_str());
    return true;
}

/**
 * @brief  保存文件
 * @return true 
 * @return false 
 * @author Jimmy 
 * @date   2021.09.17
 */
bool CsvOps::save()
{
    if (mFd == nullptr)
    {
        return false;
    }

    fflush(mFd);
    fclose(mFd);
    mFd = nullptr;
    return true;
}


/* End of file */